package fr.istic.linkedcv.client.presentation.views.impl;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

import fr.istic.linkedcv.client.CVCli;
import fr.istic.linkedcv.client.presentation.Ui;
import fr.istic.linkedcv.client.presentation.views.View;
import fr.istic.linkedcv.client.services.CVService;
import fr.istic.linkedcv.client.services.CVServiceAsync;

public class CVView extends View {
	private HTML html = new HTML();
	private final CVServiceAsync cvServiceAsync = GWT.create(CVService.class);
	public CVView(Ui ui) {
		super(ui);
	}
	@Override
	public void build() {
		String personneName = (properties.get("parameter1")!=null)?properties.get("parameter1"):ui.getControle().getPersonneCli().getLogin();
		
		cvServiceAsync.getCV(personneName, new AsyncCallback<CVCli>() {
			
			@Override
			public void onSuccess(CVCli result) {
				StringBuffer strBuffer = new StringBuffer();
				if(result!=null){
					
					strBuffer.append("<h1 class='titre_cv'>Objectifs</h1>");
					strBuffer.append("<div>"+result.getObjectif()+"</div>");
					strBuffer.append("<h1 class='titre_cv'>Études</h1>");
					strBuffer.append("<div>"+result.getEducation()+"</div>");
					strBuffer.append("<h1 class='titre_cv'>Expériences professionnelles</h1>");
					strBuffer.append("<div>"+result.getExperience_pro()+"</div>");
					strBuffer.append("<h1 class='titre_cv'>Compétences</h1>");
					strBuffer.append("<div>"+result.getCompetences()+"</div>");
					strBuffer.append("<h1 class='titre_cv'>Divers</h1>");
					strBuffer.append("<div>"+result.getDivers()+"</div>");
				}else{
					strBuffer.append("Le cv n'existe pas");
				}
				html.setHTML(strBuffer.toString());
				
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("failure");
				
			}
		});
	}

	@Override
	public void addProperties(Map<String, String> properties) {
		// TODO Auto-generated method stub
		super.addProperties(properties);
	}
	@Override
	public void addProperty(String key, String value) {
		// TODO Auto-generated method stub
		super.addProperty(key, value);
	}
	@Override
	public Widget getWidget() {
		
		return html;
	}

}
